/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/13
 */
package cn.vpclub.admin.web.home;

import cn.vpclub.admin.entity.UserEntity;
import cn.vpclub.admin.mapper.UserMapper;
import cn.vpclub.admin.util.QRCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.List;

@Controller
@RequestMapping(value = "share")
public class ShareController {
    @Autowired
    UserMapper userMapper;

    /**
     * 二维码
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "QRCodeBlog")
    public void GetQRCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestParam String url) throws Exception {
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("image/jpeg");
        String userId = (String) httpServletRequest.getAttribute("userId");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        List<UserEntity> list = userMapper.selectList(queryWrapper);
        /*头像*/
        String picture = list.get(0).getPicture();
        BufferedImage bufferedImage = QRCodeUtil.encode(url, picture, true);
        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
        ImageIO.write(bufferedImage, "jpg", servletOutputStream);
        try {
            servletOutputStream.flush();
        } finally {
            servletOutputStream.close();
        }
    }
}
